package com.boyuan.system.controller;

import com.boyuan.system.common.BaseResponse;
import com.boyuan.system.common.R;
import com.boyuan.system.model.vo.AjaxResult;
import com.boyuan.system.model.vo.LoginVo;
import com.boyuan.system.model.vo.LogoutIdRequestVo;
import com.boyuan.system.service.impl.HkCameraService;
import com.boyuan.system.utils.AssertUtils;
import com.boyuan.system.utils.PreviewUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 海康摄像头业务层
 *
 * @author ply
 * @date 2023-11-20 15:05
 */
@RestController
@RequestMapping("/hkCamera")
public class HkCameraController {

    @Resource
    private HkCameraService hkCameraService;

    /**
     * 登录并开启预览
     */
    @PostMapping("/loginAndPlayView")
    public AjaxResult loginAndPlayView(@RequestBody LoginVo loginVo) {
        AssertUtils.notNull(loginVo.getIp(), "ip地址必填");
        AssertUtils.notNull(loginVo.getUsername(), "用户名必填");
        AssertUtils.notNull(loginVo.getPassword(), "密码必填");
        AssertUtils.notNull(loginVo.getPort(), "端口必填");
        AssertUtils.notNull(loginVo.getChannelId(),"通道号必填");
        return AjaxResult.success(PreviewUtil.loginAndPlayView(loginVo));
    }

    /**
     * 退出预览和登录
     */
    @GetMapping("/logoutPlayView")
    public AjaxResult logoutPlayView(String userId, String playId) {
        AssertUtils.notNull(userId, "登录id必填");
        if (playId != null) {
            PreviewUtil.logoutPlayView(userId, playId);
        } else {
            PreviewUtil.logoutPlayView(userId);
        }
        return AjaxResult.success();
    }

    /**
     * 退出所有账号预览和登录
     */
    @PostMapping("/logoutPlayViewAll")
    public AjaxResult logoutPlayViewAll(@RequestBody List<LogoutIdRequestVo> logoutIdRequestVoList) {
        if (logoutIdRequestVoList.size() > 0) {
            PreviewUtil.logoutPlayViewAll(logoutIdRequestVoList);
        }
        return AjaxResult.success();
    }

    /**
     * 登录(判断是否在线)，只登录不预览
     * ---支持批量登录
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody List<LoginVo> loginVoList) {
        if (loginVoList.size() == 0) {
            return AjaxResult.error("登录信息必填");
        }
        return AjaxResult.success(PreviewUtil.loginCamera(loginVoList));
    }

    /**
     * 预览某个摄像头
     */
    @PostMapping("/playView")
    public AjaxResult playView(@RequestBody LoginVo loginVo) {
        AssertUtils.notNull(loginVo.getUserId(), "登录id必填");
        //AssertUtils.notNull(loginVo.getPlayId(), "预览id必填");
        return AjaxResult.success(PreviewUtil.playViewCamera(loginVo));
    }

    /**
     * 只关闭某个摄像头预览，但不退出登录
     */
    @PostMapping("/logoutPlayViewOnly")
    public AjaxResult logoutPlayViewOnly(@RequestBody LoginVo loginVo) {
        AssertUtils.notNull(loginVo.getUserId(), "登录id必填");
        AssertUtils.notNull(loginVo.getPlayId(), "预览id必填");
        return AjaxResult.success(PreviewUtil.logoutPlayViewOnly(loginVo));
    }

    @ApiOperation("获取设备通道信息")
    @PostMapping("/getChannel")
    public BaseResponse getChannel(LoginVo loginVo) {
        return R.success(PreviewUtil.getChannel(loginVo));
    }

    /**
     * 云台控制开始
     */
    @ApiOperation("云台控制开始")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "command", value = "云台动作指令", paramType = "query", dataType = "String",dataTypeClass = String.class),
    })
    @PostMapping("/playControlStart")
    public BaseResponse playControlStart(LoginVo loginVo,String command) throws InterruptedException {
        AssertUtils.notNull(loginVo.getIp(), "ip地址必填");
        AssertUtils.notNull(loginVo.getUsername(), "用户名必填");
        AssertUtils.notNull(loginVo.getPassword(), "密码必填");
        AssertUtils.notNull(loginVo.getPort(), "端口必填");
        AssertUtils.notNull(command, "云台动作指令");
        return R.success(hkCameraService.playControlStart(loginVo,command));
    }

    /**
     * 云台控制结束
     */
    @ApiOperation("云台控制结束")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "command", value = "云台动作指令", paramType = "query", dataType = "String",dataTypeClass = String.class),
    })
    @PostMapping("/playControlStop")
    public BaseResponse playControlStop(LoginVo loginVo,String command) throws InterruptedException {
        AssertUtils.notNull(loginVo.getIp(), "ip地址必填");
        AssertUtils.notNull(loginVo.getUsername(), "用户名必填");
        AssertUtils.notNull(loginVo.getPassword(), "密码必填");
        AssertUtils.notNull(loginVo.getPort(), "端口必填");
        AssertUtils.notNull(command, "云台动作指令");
        return R.success(hkCameraService.playControlStop(loginVo,command));
    }

    /**
     * 实时温度获取
     * @param loginVo
     * @return
     */
    @PostMapping("/getTemperature")
    public BaseResponse getTemperature(LoginVo loginVo) throws InterruptedException {
        AssertUtils.notNull(loginVo.getIp(), "ip地址必填");
        AssertUtils.notNull(loginVo.getUsername(), "用户名必填");
        AssertUtils.notNull(loginVo.getPassword(), "密码必填");
        AssertUtils.notNull(loginVo.getPort(), "端口必填");
        return R.success(hkCameraService.getTemperature(loginVo));
    }

}
